﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WebCalls;
using ArmyCreator.ViewModels;

namespace ArmyCreator.Pages
{
    public partial class ArmyPage : PhoneApplicationPage
    {
        private ProgressIndicator pi;
        private int id;

        public ArmyPage()
        {
            InitializeComponent();

            this.DataContext = this;

            pi = new ProgressIndicator();
            pi.IsIndeterminate = true;
            pi.IsVisible = true;
            pi.Text = "Chargement des armées...";
            SystemTray.SetProgressIndicator(this, pi);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            this.id = 15016;

            UpdateDataContext();

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                retrieveListing(15016);
            });
        }

        private void UpdateDataContext()
        {
            var listing = new ListingViewModel();
            listing.LoadData( UserContext.Instance.GetListing(this.id), 10);
            this.DataContext = listing.Army;
        }

        private async void retrieveListing(int id)
        {
            await RetrieveListing.Get(id);

            UpdateDataContext();

            pi.IsVisible = false;
        }
    }
}